﻿using System;
using System.IO;
using WebCrawlerLib.Common;

namespace WebCrawlerLib
{
    public class BingWallPaperService
    {
        #region 字段

        private string _directoryName;

        #endregion

        public BingWallPaperService(string directoryName)
        {
            _directoryName = directoryName;
        }

        /// <summary>
        /// 启动今日抓包
        /// </summary>
        public void Start()
        {
            var filePath = string.Format("{0}\\{1}.jpg", _directoryName, DateTime.Now.ToString("yyyy-MM-dd"));
            if (File.Exists(filePath))
                return;

            // 1.抓取bing.com
            var html = GetHtmlFromBing();
            // 2.抓取图片Url
            string urlPicture = BingWallPaperUtility.GetPictureUrl(html);

            // 3.下载图片
            urlPicture = urlPicture.Replace("\\u0026", "&").Replace("\\", "");

            var uhdPicture = urlPicture.Substring(0, urlPicture.IndexOf("_1920x1080")) + "_UHD.jpg&rf=LaDigue_UHD.jpg&w=3840&h=2160&c=8&rs=1&o=3&r=0";
            var uhdFilePath = string.Format("{0}\\{1}.4K.jpg", _directoryName, DateTime.Now.ToString("yyyy-MM-dd"));

            try
            {
                var picResponse = HttpWebResponseUtility.CreateGetHttpResponse(urlPicture, null, null, null);
                using (Stream stream = picResponse.GetResponseStream())
                {
                    StreamWriter sw = new StreamWriter(filePath); //创建一个stream读取流
                    stream.CopyTo(sw.BaseStream);
                    sw.Flush();
                    sw.Close();
                }
            }
            catch (Exception ex)
            {
                throw new Exception($"urlPicture: {urlPicture}", ex);
            }

            try
            {
                var picResponse = HttpWebResponseUtility.CreateGetHttpResponse(uhdPicture, null, null, null);
                using (Stream stream = picResponse.GetResponseStream())
                {
                    StreamWriter sw = new StreamWriter(uhdFilePath); //创建一个stream读取流
                    stream.CopyTo(sw.BaseStream);
                    sw.Flush();
                    sw.Close();
                }
            }
            catch (Exception ex)
            {
                throw new Exception($"urlPicture: {urlPicture}", ex);
            }
            // 4.更换壁纸
            // 手动更换
        }

        private string GetHtmlFromBing()
        {
            string tagUrl = System.Configuration.ConfigurationManager.AppSettings["BingWallPaperJob_TagUrl"];
            string html;
            try
            {
                var response = HttpWebResponseUtility.CreateGetHttpResponse(tagUrl, null, null, null);
                using (Stream stream = response.GetResponseStream())//获取响应的字符串流
                {
                    StreamReader sr = new StreamReader(stream); //创建一个stream读取流
                    html = sr.ReadToEnd();   //从头读到尾，放到字符串html
                }
            }
            catch (Exception ex)
            {
                throw new Exception($"tagUrl: {tagUrl}", ex);
            }
            return html;
        }
    }
}
